import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Clientapp_UDP extends WindowAdapter implements ActionListener, KeyListener
{
	TextField str_send;
	Label label;
	TextArea msg;
	Button send, exit;
	Panel panel;
	
	DatagramSocket receiveSocket, sendSocket;
	DatagramPacket receivePacket, sendPacket;
	
	
	public void display()
	{
		Frame f = new Frame("Client");
		f.setSize(400, 350);
		f.setLocation(100, 100);
		f.setBackground(Color.blue);
		panel = new Panel();
		f.add(panel, "South");
		
		msg = new TextArea();
		msg.setSize(100, 250);
		msg.setBackground(Color.white);
		msg.setEditable(false);
		f.add(msg);
		
		label = new Label("send");
		panel.add(label);
		
		str_send = new TextField(20);
		panel.add(str_send);
		str_send.addKeyListener(this);
		
		send = new Button("send");
		panel.add(send);
		send.addActionListener(this);
		
		exit = new Button("exit");
		panel.add(exit);
		exit.addActionListener(this);
		
		f.addWindowListener(this);
		f.setVisible(true);
		try
		{
			sendSocket = new DatagramSocket(3000);
		}
		catch (Exception e)
		{
			msg.append(e+"\n");
		}
	}


	public void keyTyped(KeyEvent e)
	{
	}

	public void keyPressed(KeyEvent e)
	{
		if (e.getSource() == str_send)
			if (e.getKeyChar() == KeyEvent.VK_ENTER)
				sendMessage();		
	}

	public void keyReleased(KeyEvent e)
	{
	}

	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == send)
			sendMessage();
		if (e.getSource() == exit)
			System.exit(0);		
	}
	
	public void windowClosing(WindowEvent e)
	{
		System.exit(0);
	}
	
	public void receiveMessage()
	{
		try
		{
			receiveSocket = new DatagramSocket(3001);
			while (true)
			{
				byte[] buf = new byte[500];
				receivePacket = new DatagramPacket(buf, buf.length);
				receiveSocket.receive(receivePacket);
				
				if (receivePacket.getLength() == 0)
				{
					continue;
				}
				ByteArrayInputStream bin = new ByteArrayInputStream(receivePacket.getData());
				BufferedReader reader = new BufferedReader(new InputStreamReader(bin));
				
				msg.append("server : " + reader.readLine());
				msg.append("\n");
				reader.close();
				bin.close();
			}
		}
		catch (Exception e)
		{
			msg.append(e + "\n");
		}
	}
	
	public void sendMessage()
	{
		try
		{
			String s = str_send.getText();
			str_send.setText("");
			if (s.length() != 0)
				msg.append("client : " + s + "\n");
			
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			// PrintStream pout = new PrintStream(out);
			// pout.print(s);
			
			byte buf[] = out.toByteArray();
			sendPacket = new DatagramPacket(buf, buf.length, InetAddress.getByName("172.18.32.45"), 3333);
//			sendPacket = new DatagramPacket(buf, buf.length, InetAddress.getByName("localhost"), 3333);
			sendSocket.send(sendPacket);
		}
		catch (Exception e)
		{
			msg.append(e + "\n");
		}
	}
	
	public static void main(String a[])
	{
		Clientapp_UDP app = new Clientapp_UDP();
		app.display();
		app.receiveMessage();
	}
}
